home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / LASRLINE.ARJ / LASER.C next >
C/C++ Source or Header  |  1991-04-14  |  5KB  |  268 lines

  1. #include <graphics.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <dos.h>
  5. #include <conio.h>
  6. #include <time.h>
  7.  
  8. #define MAXINDEX 150
  9. #define C1 50 
  10. #define C2 10
  11. #define C3 5
  12.  
  13. struct linerec
  14. {
  15.     int x1, x2, y1, y2, color;
  16. };
  17.  
  18. struct linerec linearray[MAXINDEX];
  19.  
  20. int maxstore = 0;
  21.  
  22. void eraseline(int index)
  23. {
  24.     setcolor(0);
  25.     line(linearray[index].x1, linearray[index].x2, 
  26.     linearray[index].y1, linearray[index].y2);
  27. }
  28.  
  29. int maxcolor;
  30.  
  31. void newcolor(int *color, int *count1)
  32. {
  33.     *color = random(maxcolor-1) + 1;
  34.     *count1 = random(C1) + 1;
  35. }
  36.  
  37. void newstep (int *dx1, int *dy1, int *dx2, int *dy2, int *count2)
  38. {
  39.     *dx1 = random(C2) - C3;
  40.     *dx2 = random(C2) - C3;
  41.     *dy1 = random(C2) - C3;
  42.     *dy2 = random(C2) - C3;
  43.     *count2 = random(C1) + 1;
  44. }
  45.  
  46. void newcoord(int *n, int *change, int min, int max)
  47. {
  48.     int temp;
  49.  
  50.     temp = *n + *change;
  51.     if (temp < min || temp > max) *change = -*change;
  52.     else *n = temp;
  53. }
  54.  
  55. int xmin = 0, ymin = 0, xmax, ymax;
  56. int dy1, dy2, dx1, dx2;
  57.  
  58.  
  59. void computenew(int *lx1, int *lx2, int *ly1, int *ly2)
  60. {
  61.     newcoord(lx1, &dx1, xmin, xmax);
  62.     newcoord(ly1, &dy1, ymin, ymax);
  63.     newcoord(lx2, &dx2, xmin, xmax);
  64.     newcoord(ly2, &dy2, ymin, ymax);
  65. }
  66.  
  67.  
  68. void storedata(int xx1, int xx2, int yy1, int yy2, int color, int index)
  69. {
  70.     linearray[index].x1 = xx1;
  71.     linearray[index].x2 = xx2;
  72.     linearray[index].y1 = yy1;
  73.     linearray[index].y2 = yy2;
  74.     linearray[index].color = color;
  75.     if (index > maxstore) maxstore = index;
  76. }
  77.  
  78. int delay_time;
  79.  
  80. long timemark(void)
  81. {
  82.     struct time mytime;
  83.  
  84.     gettime(&mytime);
  85.  
  86.     return mytime.ti_min * 6000L + mytime.ti_hour * 360000L
  87.        + mytime.ti_sec * 100 + mytime.ti_hund;
  88. }
  89.  
  90. void setspeed(void)
  91. {
  92.     int i;
  93.  
  94.     long time1, time2;
  95.  
  96.  
  97.     time1 = timemark();
  98.  
  99.        
  100.     for ( i = 0; i < 100; i++)
  101.     {
  102.     setcolor(1);
  103.     line(xmin, ymin, xmax, ymax);
  104.     setcolor(0);
  105.     line(xmin, ymin, xmax, ymax);
  106.     }
  107.     
  108.     time2 = timemark();
  109.  
  110.     if (time1 > time2) setspeed();
  111.     else delay_time = 2000/(time2 - time1);
  112.  
  113. }
  114.  
  115. void main(void)
  116. {
  117.     int graphdriver = 0; /* IBM8514; for use with 8514 */
  118.     int graphmode = 0; /* 1; */
  119.     struct palettetype far *pal = NULL;
  120.     int i, errorcode, erasing = 0, index = 0, color;
  121.     int lx1, lx2, ly1, ly2, count1 = 0, count2 = 0;
  122.     int ch;
  123.     char far *ptr;
  124.  
  125.  
  126.     char buff[100];
  127.  
  128.     randomize();
  129.  
  130.     registerbgidriver(CGA_driver);
  131.     registerbgidriver(EGAVGA_driver);
  132.     registerbgidriver(Herc_driver);
  133.     registerbgidriver(ATT_driver);
  134.     registerbgidriver(PC3270_driver);
  135.     registerbgidriver(IBM8514_driver);
  136.  
  137.  
  138.     initgraph(&graphdriver, &graphmode, NULL);
  139.  
  140.  
  141.     errorcode = graphresult();
  142.  
  143.     if (errorcode != grOk)
  144.     {
  145.     puts("\nError: ");
  146.     switch (errorcode)
  147.     {
  148.         case grNotDetected :
  149.         puts("Cannot detect a graphics card");
  150.         break;
  151.  
  152.         case grFileNotFound :
  153.         puts("Cannot find driver file");
  154.         break;
  155.  
  156.         case grInvalidDriver :
  157.         puts("Invalid Driver");
  158.         break;
  159.  
  160.         case grNoLoadMem :
  161.         puts("Insufficient memory to load driver");
  162.         break;
  163.     }
  164.     exit(1);
  165.     }
  166.  
  167.     pal = getdefaultpalette();
  168.  
  169.     maxcolor =    pal->size;
  170.  
  171.     xmax = getmaxx();
  172.     ymax = getmaxy();
  173.  
  174.     i = 1;
  175.     outtextxy(0, i, ptr = "Welcome to LASERGRAPH!");
  176.     i += textheight(ptr);
  177.     outtextxy(0, i, ptr = "Pressing the <ESC> key");
  178.     i += textheight(ptr);
  179.     outtextxy(0, i, ptr = "will end the program.");
  180.     i += textheight(ptr);
  181.     outtextxy(0, i, ptr = "Pressing the <Space> bar");
  182.     i += textheight(ptr);
  183.     outtextxy(0, i, ptr = "will freeze the action.");
  184.     i += textheight(ptr);
  185.     outtextxy(0, i, ptr = "Press any key to continue.");
  186.     i += textheight(ptr);
  187.     outtextxy(0, i, ptr = "Press <+> to speed up.");
  188.     i += textheight(ptr);
  189.     outtextxy(0, i, ptr = "Press <-> to slow down.");
  190.     i += textheight(ptr);
  191.     outtextxy(0, i, ptr = "Press <C> to clear the screen.");
  192.     i += textheight(ptr);
  193.  
  194.     outtextxy(0, i, ptr = "To begin the show press any key...");
  195. /*
  196.     sprintf(buff, "maxx is %d, maxy is %d, mode is %d", xmax, ymax, graphdriver);
  197.     i += 10;
  198.     outtextxy(0, i, buff);
  199. */
  200.  
  201.     getch();
  202.     cleardevice();
  203.  
  204.     setspeed();
  205.  
  206.     cleardevice();
  207.  
  208.     lx2 = lx1 = xmin;
  209.     ly1 = ly2 = ymin;
  210.  
  211.  
  212.     do
  213.     {
  214.     ch = 0;
  215.     if (index >= MAXINDEX-1)
  216.     {
  217.         index = 0;
  218.         erasing = 1;
  219.     }
  220.  
  221.      if (erasing) eraseline(index);
  222.  
  223.     if (count1 == 0) newcolor(&color, &count1);
  224.  
  225.         if (count2 == 0) newstep(&dx1, &dy1, &dx2, &dy2, &count2);
  226.  
  227.     count1--;
  228.     count2--;
  229.  
  230.     computenew(&lx1, &lx2, &ly1, &ly2);
  231.     setcolor(color);
  232.     line(lx1, lx2, ly1, ly2);
  233.        
  234.     storedata(lx1, lx2, ly1, ly2, color, index++);
  235.  
  236.     while (kbhit()) ch = getch();
  237.     {
  238.             switch (ch)
  239.             {
  240.                 case ' ' : 
  241.             ch = getch();
  242.             break;
  243.         
  244.         case '+' : 
  245.             if (delay_time > 0) delay_time--;
  246.             break;
  247.         
  248.         case '-' :
  249.             if (delay_time < 100) delay_time++;
  250.             break;
  251.  
  252.         case 'c' :
  253.         case 'C' :
  254.             cleardevice();
  255.             erasing = 0;
  256.             index = 0;
  257.             break;
  258.  
  259.         }
  260.     }
  261.     
  262.     delay(delay_time);
  263.     }
  264.     while (ch != 27);
  265.  
  266.     closegraph();
  267.  
  268. }